package com.beta.DB;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;


public class DBHelper extends SQLiteOpenHelper{
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "NFC_DATA";
	private static final String CREATE_REPLAYS_TABLE = "CREATE TABLE card_info(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
			"bank_name TEXT,ID TEXT UNIQUE,user_name TEXT,savetime TEXT, transactions BLOB," +
			" type INTEGER,picture_id INTEGER, built_in INTEGER DEFAULT 0)";
	//_id,bank_name,ID,user_name,savetime,transactions,type,picture_id,built_in
	private static final String TABLE_NAME = "card_info";
	private SQLiteDatabase db;
	public DBHelper(Context context){
		super(context,DATABASE_NAME,null,DATABASE_VERSION);
	}
	@Override 
	public void onCreate(SQLiteDatabase db){
		this.db = db;
		db.execSQL(CREATE_REPLAYS_TABLE);
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	//follow is save data;
	public void saveData(SQLiteDatabase db,String bank_name,int ID,String user_name,byte[][] transactions,int type,int picture_R_id,int built_in){
		SimpleDateFormat turn = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
		Date nowtime = new Date(System.currentTimeMillis());
		String savetime = turn.format(nowtime);
		
		ContentValues cv = new ContentValues();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(transactions);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] tBytes = baos.toByteArray();
		
		cv.put("transactions", tBytes);
		cv.put("bank_name",bank_name);
		cv.put("ID",ID);
		cv.put("user_name", user_name);
		cv.put("type",type);
		cv.put("built_in", built_in);
		cv.put("savetime", savetime);
		cv.put("picture_id",picture_R_id);
		this.insert_info(cv);
	}
	public void insert_info(ContentValues values){
		SQLiteDatabase db = this.getWritableDatabase();
		db.insert(TABLE_NAME, null, values);
		db.close();
	}
	/**
	 * 以数据库中的id为查询条件查询
	 * @param id
	 * @return
	 */
	public Cursor query(String id){
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor c = db.query(TABLE_NAME, null,"_id = "+id,null, null, null,null);
		if(c == null ){
			return null;
		}
		return c;
	}
	/**
	 * 以卡号为条件查询
	 * @param id
	 * @return
	 */
	public Cursor queryWithCardNumber(String id){
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor c = db.query(TABLE_NAME, null,"ID = "+id,null, null, null,null);
		return c;
	}
	/**
	 * delete a card from the table "card_info"
	 * @param id of the card
	 */
	public void del(String id){
		SQLiteDatabase db = this.getWritableDatabase();
  		db.delete(TABLE_NAME, "_id=?", new String[] {String.valueOf(id)});
	}
	
	public ArrayList<Integer> getPictures(){
		SQLiteDatabase db=this.getReadableDatabase();
		ArrayList<Integer> list=new ArrayList<Integer>();
		Cursor cur=db.query(TABLE_NAME, null, null, null, null, null, null);
		if(cur!=null){
			cur.moveToFirst();
			do{
				int pic_id=cur.getInt(cur.getColumnIndex("picture_id"));
				list.add(Integer.valueOf(pic_id));
			}while(cur.moveToNext());
		}
		return list;
	}
	/**
	 * 为空的话返回true
	 * @return
	 */
	public boolean isEmpty(){
		SQLiteDatabase db=this.getReadableDatabase();
		Cursor c=db.query(TABLE_NAME, null, null, null, null, null, null);
		if(c==null)
			return true;
		if(!c.moveToFirst())
			return true;
		if(c.getCount()>0)
			return false;
		else
			return true;
	}
	/**
	*是否包含卡号为ID的支付卡信息，包含的话返回true,否则false
	*/
	/*public boolean contains(String ID){
		SQLiteDatabase db=this.getWritableDatabase();
		Cursor cur=db.queryWithCardNumber(ID);
		return cur==null?false:true;
	}
	/*public String[] BankInformationGet(Cursor card){
		ImageView card_image=(ImageView)findViewById(card.getInt(6));
		String bank_name=card.getInt(2)锛�
	}*/
}